<template>
  <el-row :gutter="10">
    <el-col :span="12">
      <EditOnlineinquirySheet
        v-bind:onlineInquirySheetDataParent="onlineInquirySheetData"
      ></EditOnlineinquirySheet>
    </el-col>
    <el-col :span="12">
      <div v-if="index <= 3">
        <div
          v-if="
            onlineInquirySheetData.onlineInquiryReplys &&
              onlineInquirySheetData.onlineInquiryReplys.length > 0
          "
        >
     
          <OnlineInquiryReply
            v-for="(item,index) in onlineInquirySheetData.onlineInquiryReplys"
            :key="item.id"
            :ref='`reply_${index}`'
            v-bind:onlineInquiryReplyParent="item"
            v-bind:onlineInquirySheetSimpleData="onlineInquirySheetSimpleData"
            :onlineInquiryReplys.sync="
              onlineInquirySheetData.onlineInquiryReplys
            "
            @saveAll='saveAll'
          ></OnlineInquiryReply>
        </div>
      </div>
      <div v-else style="font-size:20px;color:red;">
        已有3个方案，不允许再添加
      </div>
    </el-col>
  </el-row>
</template>
<script>
import EditOnlineinquirySheet from "./editOnlineinquirySheet";
import OnlineInquiryReply from "./onlineInquiryReply";
import { ajax } from "@/store/yg/common/utils";
import { clone } from "@/utils/clone";
export default {
  components: { EditOnlineinquirySheet, OnlineInquiryReply },
  props: ["data", "justShowOnlineSheet", "justAddOneReply"],
  data() {
    return {
      onlineInquirySheetData: {},
      onlineInquirySheetSimpleData: {
        onlineInquirySheetId: null,
        onlineInquiryShippingWay: null,
        version: null,
        chargeWeight: null,
        customerVolume: null,
        customerActualWeight: null,
        defaultChannelId: null,
        chargingTypeMap: {},
        amoebaSubjectMap: {},
        channelNameMap: {},
        defaultChargingMode: {},
        channelIdAndChargingModeMap: {},
        channelShippingWayMap: {}
      },
      index: 1
    };
  },
  mounted() {
    if (this.$route.query.pid) {
      const params = { id: this.$route.query.pid };
      this.findOnlineInquirySheetAndReply(params);
    } else if (this.$route.query.replyId) {
      const params = { replyId: this.$route.query.replyId };
      this.findOnlineInquirySheetAndReply(params);
    }
  },
  methods: {
    async saveOrUpdateOnlineInquiryReplyAll(list){
      const data = await ajax("saveOrUpdateOnlineInquiryReplyList", list);
        if (data.code == 200) {
          this.$message.success("全部保存成功");
          this.onlineInquiryReplyData.id = data.data;
          window.open(`#/main/waybill__onlineinquiry__list`)
        } else if (data.code == 300) {
          this.$confirm(m(data.message), "警告", {
            confirmButtonText: "强制保存",
            cancelButtonText: "取消",
            type: "warning"
          })
            .then(_ => {
              params["forceUpdate"] = 1;
              this.saveOrUpdateOnlineInquiryReplyAll(params);
            })
            .catch(_ => {});
        } else {
          this.$message.error(m(data.message));
        }
     },
    saveAll(){
      let length= this.onlineInquirySheetData.onlineInquiryReplys.length
      console.log('length',length);
      const list = []
      for (let index = 0; index < length; index++) {
        console.log();
        const data = clone(this.$refs[`reply_${index}`][0].onlineInquiryReplyData)
        console.log('data',data);
        list.push(data)
      }
      this.saveOrUpdateOnlineInquiryReplyAll(list)
    },
    async findOnlineInquirySheetAndReply(params) {
      const data = await ajax("findOnlineInquirySheetAndReplyById", params, {
        uri: "/waybill/onlineinquiry/list",
        type: "main"
      });
      const query = {
        onlineInquiryShippingWay: data.onlineInquiryShippingWay,
        chargeWeight: data.chargeWeight,
        volume: data.customerVolume
      };
      let defaultChannelId = null;
      if (data.instockChannelIds && data.instockChannelIds.length == 1) {
        query["channelId"] = data.instockChannelIds[0];
        defaultChannelId = data.instockChannelIds[0];
      }
      const replySource = await ajax("initOnlineInquiryReplySource", query, {
        uri: "/waybill/onlineinquiry/list",
        type: "main"
      });
      if (
        !this.justShowOnlineSheet &&
        (!data.onlineInquiryReplys ||
          data.onlineInquiryReplys.length == 0 ||
          this.justAddOneReply)
      ) {
        this.index = data.onlineInquiryReplys.length + 1;
        const onlineInquiryReplyParent = {};
        onlineInquiryReplyParent.onlineInquirySheetId = data.id;
        onlineInquiryReplyParent.index = this.index;
        onlineInquiryReplyParent.isAccept = 1;
        onlineInquiryReplyParent.isQuotationList = 1;
        if (replySource.amoebaSubjectMap) {
          var onlineInquiryCosts = new Array();
          for (const key in replySource.amoebaSubjectMap) {
            var onlineInquiryCost = new Object();
            onlineInquiryCost["amoebaSubjectId"] = parseInt(key);
            onlineInquiryCosts.push(onlineInquiryCost);
          }
          onlineInquiryReplyParent.onlineInquiryCosts = onlineInquiryCosts;
        }
        if (this.justAddOneReply) {
          const onlineInquiryReplys = new Array();
          onlineInquiryReplys.push(onlineInquiryReplyParent);
          data.onlineInquiryReplys = onlineInquiryReplys;
        } else {
          data.onlineInquiryReplys.push(onlineInquiryReplyParent);
        }
      }
      this.onlineInquirySheetData = data;
      this.onlineInquirySheetSimpleData = replySource;
      this.onlineInquirySheetSimpleData.onlineInquirySheetId = data.id;
      this.onlineInquirySheetSimpleData.onlineInquiryShippingWay =
        data.onlineInquiryShippingWay;
      this.onlineInquirySheetSimpleData.chargeWeight = data.chargeWeight;
      this.onlineInquirySheetSimpleData.customerVolume = data.customerVolume;
      this.onlineInquirySheetSimpleData.customerActualWeight =
        data.customerActualWeight;
      this.onlineInquirySheetSimpleData.version = data.version;
      this.defaultChargingMode = data.defaultChargingMode;
      if (this.justShowOnlineSheet) {
        this.onlineInquirySheetSimpleData.justShowOnlineSheet = this.justShowOnlineSheet;
      }
      if (defaultChannelId) {
        this.onlineInquirySheetSimpleData.defaultChannelId = defaultChannelId;
      }
    }
  }
};
</script>
<style scoped>
.p_ms {
  line-height: 30px;
  margin-left: 28px;
}
</style>
